<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>查看库存</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../frame/layui/css/modules/layer/default/layer.css">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>查看库存</legend>
</fieldset>

<div class="tail">
    <div class="head">

        <div class="my-btn-box" style="margin-left: 50px;">
            <a href="addstock.jsp" style="color: white;">
                <button class="layui-btn layui-btn-sm">新增配件</button>
            </a>
            <div style="float: right; margin-right: 50px;">
    <span class="fr">
        <span class="layui-form-label">模糊查询：</span>
        <div class="layui-input-inline">
            <input type="text" id="sousuo" autocomplete="off" placeholder="请输入配件名或品牌" class="layui-input">
        </div>
        <button class="layui-btn mgl-20" onclick="queryByKey()">查询</button>
    </span>
            </div>
        </div>

        <hr/>
    </div>
</div>
<table class="layui-table" lay-skin="line" id="test">
    <colgroup>
        <col width="155">
        <col width="300">
        <col width="155">
        <col width="200">
        <col width="100">
        <col width="100">
        <col width="100">
        <col width="155">
    </colgroup>
    <thead>
    <tr>
        <th>库存编号</th>
        <th>配件名称</th>
        <th>品牌</th>
        <th>型号</th>
        <th>进价</th>
        <th>售价</th>
        <th>库存数量</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="table_tbody">
    </tbody>
</table>
<script src="../js/jquery.js"></script>
<script type="text/javascript" src="../frame/layui/layui.js"></script>
<script src="../frame/layui/lay/modules/layer.js"></script>
<script src="../js/dateToString.js"></script>

<script>
    $(function () {
        // 加载数据
        loadDate(null,1,6);

        $(".prev").live("click",function () {
            var keywords = $("#sousuo").val();
            // 计算上一页
            var pageNo = $(".current_num").text();
            pageNo = parseInt(pageNo);
            if (pageNo == undefined || pageNo <= 1){
                pageNo = 1;
            }else{
                pageNo = pageNo - 1;
            }
            // 加载数据
            loadDate(keywords,pageNo,6);
        });

        $(".next").live("click",function () {
            var keywords = $("#sousuo").val();
            var pageNo = $(".current_num").text();
            pageNo = parseInt(pageNo);
            var totalNo = $(".total_num").text();
            totalNo = parseInt(totalNo);
            if (pageNo == undefined){
                pageNo = 1;
            } else if(pageNo >= totalNo){
                pageNo = totalNo;
            }else{
                pageNo = pageNo + 1;
            }
            // 加载数据
            loadDate(keywords,pageNo,6);
        });

        $(".first").live("click",function () {
            var keywords = $("#sousuo").val();
            var pageNo = 1;
            // 加载数据
            loadDate(keywords,pageNo,6);
        });

        $(".last").live("click",function () {
            var keywords = $("#sousuo").val();
            var pageNo = $(".total_num").text();
            pageNo = parseInt(pageNo);
            if (pageNo == undefined){
                pageNo = 1;
            }
            // 加载数据
            loadDate(keywords,pageNo,6);
        });
    });

    function loadDate(keywords,pageNo,pagesize) {
        layer.load(1);

        $("#table_tbody").empty();
        $.ajax({
            url: "/stock/querystock",
            type: "POST",
            data: {
                keywords:keywords,
                pagenum:pageNo,
                pagesize:pagesize,
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                $("#sousuo").val(keywords);
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 200);
                if(data.success){
                    var stocks = data.data.list;
                    for (var i = 0; i < stocks.length; i++) {
                        $("#table_tbody").append("" +
                            "<tr>" +
                            "<td>" + stocks[i].repid + "</td>" +
                            "<td>" + stocks[i].repname + "</td>" +
                            "<td>" + stocks[i].repdrand + "</td>" +
                            "<td>" + stocks[i].repmodel + "</td>" +
                            "<td>" + stocks[i].reporiginal + "</td>" +
                            "<td>" + stocks[i].repmoney + "</td>" +
                            "<td>" + stocks[i].repnum + "</td>" +
                            "<td><a href='updatestock.jsp?"+stocks[i].repid+"'>" +
                            "<button class='layui-btn layui-btn-sm'><i class='layui-icon'>&#xe642;</i></button>" +
                            "</a>&nbsp;"+
                            "<a href='javascript:del("+stocks[i].repid+");'>" +
                            "<button class='layui-btn layui-btn-sm'><i class='layui-icon'>&#xe640;</i>" +
                            "</button></a></td>" +
                            "</tr>");
                    }
                    $("#table_tbody").append("<tr><td colspan='8'></td></tr>" +
                        "<tr style='border-bottom: 1px solid #AAAAAA; height: 50px;text-align: center;'>" +
                        "<td colspan='8'><div id='page_div'>" +
                        "第 <span class='current_num'>"+data.data.pageNum+"</span>页&nbsp;&nbsp;" +
                        "共 <span class='total_num'>" + data.data.pages + " </span>页&nbsp;&nbsp;" +
                        "每页 "+ data.data.size + " 条信息&nbsp;&nbsp;" +
                        "共 " + data.data.total + " 条信息&nbsp;&nbsp;" +
                        "&nbsp;&nbsp;<a href='javascript:(0);' class='first'>首页</a>" +
                        "&nbsp;&nbsp;<a href='javascript:(0);' class='prev'>上一页</a>" +
                        "&nbsp;&nbsp;<a href='javascript:(0);' class='next'>下一页</a>" +
                        "&nbsp;&nbsp;<a href='javascript:(0);' class='last'>尾页</a>" +
                        "</td></tr>");
                }else{
                    layer.msg(data.message);
                }
            }
        });
    }
    // 删除数据
    function del(repid) {
        layer.confirm("是否确认删除？", {
            btn: ['确认', '取消'] //按钮
        }, function(){
            $.ajax({
                url: "/stock/deletestock",
                type: "POST",
                async:false,
                data: {
                    repid:repid
                },
                dataType: "json",
                success: function (data) {
                    if(data.success){
                        layer.msg(data.message);
                    }else{
                        layer.msg(data.message);
                    }
                }
            });
            window.location.reload();
        }, function(){

        });
    }
    // 模糊查询
    function queryByKey() {
        var keywords = $("#sousuo").val();
        console.log(keywords);
        loadDate(keywords,1,6);
    }
</script>

</body>
</html>